/***
  Copyright (c) 2012 CommonsWare, LLC
  
  Licensed under the Apache License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may obtain
  a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

package com.zulu.karaoke.actionbar;

import java.util.List;

import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.zulu.karaoke.R;
import com.zulu.karaoke.constants.CommonConstants;
import com.zulu.karaoke.gui.FragmentFullListFavourite;
import com.zulu.karaoke.gui.FragmentFullListSong;

public class ActionModeHelper implements ActionMode.Callback,AdapterView.OnItemLongClickListener {
	
	private FragmentFullListFavourite hostFavouriteList;
	private FragmentFullListSong hostSongList;
	private int hostAction;
	private List<String> lstTitle;
	private List<String> lstNumber;
	private String title;
	private String number;
	private ActionMode activeMode;
	private ListView modeView;
	private int lastPosition = -1;

	public ActionModeHelper(final FragmentFullListFavourite host, ListView modeView, List<String> lstTitle, List<String> lstNumber) {
		this.hostFavouriteList = host;
		this.modeView = modeView;
		this.lstTitle = lstTitle;
		this.lstNumber = lstNumber;
		this.hostAction = CommonConstants.ActionBarConstants.ACTION_BAR_FAVOURITE;
	}
	
	public ActionModeHelper(final FragmentFullListSong host, ListView modeView, List<String> lstTitle, List<String> lstNumber) {
		this.hostSongList = host;
		this.modeView = modeView;
		this.lstTitle = lstTitle;
		this.lstNumber = lstNumber;
		this.hostAction = CommonConstants.ActionBarConstants.ACTION_BAR_SONG;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> view, View row, int position,
			long id) {
		lastPosition = position;
		title = lstTitle.get(position);
		number = lstNumber.get(position);
		modeView.clearChoices();
		modeView.setItemChecked(lastPosition, true);

		if (activeMode == null) {
			if(hostAction == CommonConstants.ActionBarConstants.ACTION_BAR_FAVOURITE)
				activeMode = hostFavouriteList.getActivity().startActionMode(this);
			else
				activeMode = hostSongList.getActivity().startActionMode(this);
		}

		return (true);
	}

	@Override
	public boolean onCreateActionMode(ActionMode mode, Menu menu) {
		if(hostAction == CommonConstants.ActionBarConstants.ACTION_BAR_FAVOURITE) {
			MenuInflater inflater = hostFavouriteList.getActivity().getMenuInflater();
			inflater.inflate(R.menu.favourite_action, menu);
		} else {
			MenuInflater inflater = hostSongList.getActivity().getMenuInflater();
			inflater.inflate(R.menu.song_action, menu);
		}
//		mode.setTitle(R.string.context_title_update_favourite);
		mode.setTitle(title);
		mode.setSubtitle(number);
		activeMode = mode;

		return (true);
	}

	@Override
	public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
		return (false);
	}

	@Override
	public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
		boolean result;
		if(hostAction == CommonConstants.ActionBarConstants.ACTION_BAR_FAVOURITE) {
			result = hostFavouriteList.performAction(item, lastPosition);
		} else {
			result = hostSongList.performAction(item, lastPosition);
		}

//		if (item.getItemId() == R.id.remove) {
			activeMode.finish();
//		}

		return (result);
	}

	@Override
	public void onDestroyActionMode(ActionMode mode) {
		activeMode = null;
		modeView.clearChoices();
		modeView.requestLayout();
	}
}